package com.hyacinth.lotterysystem.service.mq;

import com.hyacinth.lotterysystem.common.utils.JacksonUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.hyacinth.lotterysystem.common.config.DirectRabbitConfig.*;

@Component
@RabbitListener(queues = DLX_QUEUE_NAME)
public class DlxReceiver {
    private static final Logger logger = LoggerFactory.getLogger(DlxReceiver.class);

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 死信队列处理方法
     *
     * @param message
     */
    @RabbitHandler
    public void process(Map<String, String> message) {
        logger.info("[死信队列] 开始处理异常-> message: {}", message);
        rabbitTemplate.convertAndSend(EXCHANGE_NAME, ROUTING, message);
        logger.info("[死信队列] mq消息发送成功: map = {}", JacksonUtil.writeValueAsString(message));
    }
}
